package com.geek.sub;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPubSub;

public class SubscribeOrder {

    public SubscribeOrder(JedisPool jedisPool, String channelName) {
        new Thread(() -> {
            System.out.println("start subscribe order");
            try (Jedis jedis = jedisPool.getResource()) {
                jedis.subscribe(new JedisPubSub() {
                    @Override
                    public void onMessage(String channel, String message) {
                        if (message.isEmpty()) {
                            System.out.println("subscribe end");
                            System.exit(0);
                        }
                        System.out.printf("Receive message from %s :: %s\n", channel, message);
                    }
                }, channelName);
            }
        }, "subThread").start();

    }
}
